package com.reggie.module.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.common.ResultResponseVo;
import com.reggie.common.enums.front.AddressBookEnum;
import com.reggie.common.enums.front.ShoppingCartEnum;
import com.reggie.module.entity.po.AddressBook;
import com.reggie.module.entity.po.ShoppingCart;
import com.reggie.module.mapper.AddressBookMapper;
import com.reggie.module.mapper.ShoppingCartMapper;
import com.reggie.module.service.AddressBookService;
import com.reggie.module.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.reggie.common.Constant.LOGIN_SESSION;
import static com.reggie.common.Constant.USER_SESSION;

/**
 * @Author 早睡蛋
 * @Date 2022/9/28 15:23:47
 * @Desc:
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;


    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @Override
    @Transactional
    public ResultResponseVo add(ShoppingCart shoppingCart, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        Long userId = (Long) request.getSession().getAttribute(USER_SESSION);
        shoppingCart.setUserId(userId);
        shoppingCart.setCreateTime(LocalDateTime.now());
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        //判断是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        if (dishId != null) {
            shoppingCartQueryWrapper
                    .eq("dish_id", dishId)
                    .eq("user_id", userId);
            if (shoppingCart.getDishFlavor()!=null){
                shoppingCartQueryWrapper.eq("dish_flavor", shoppingCart.getDishFlavor());
            }
        } else {
            shoppingCartQueryWrapper
                    .eq("setmeal_id", shoppingCart.getSetmealId())
                    .eq("user_id", userId);
        }
        //查看是否已经存在于购物车中
        ShoppingCart isExit = shoppingCartMapper.selectOne(shoppingCartQueryWrapper);
        if (isExit != null) {
            Integer currentNumber = isExit.getNumber();
            isExit.setNumber(currentNumber + 1);
            int updateById = shoppingCartMapper.updateById(isExit);
            map.put("number", currentNumber + 1);
            if (updateById > 0) return ShoppingCartEnum.ADD_OK.getResultResponseVo().setData(map);
        } else {
            int insert = shoppingCartMapper.insert(shoppingCart);
            map.put("number", 1);
            if (insert > 0) return ShoppingCartEnum.ADD_OK.getResultResponseVo().setData(map);

        }
        return ShoppingCartEnum.ADD_FAIL.getResultResponseVo();
    }

    /**
     * 购物车信息列表展示
     *
     * @param request
     * @return
     */
    @Override
    public ResultResponseVo listByUserId(HttpServletRequest request) {
        Long userId = (Long) request.getSession().getAttribute(USER_SESSION);
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        shoppingCartQueryWrapper
                .eq("user_id", userId)
                .last("order by create_time");
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(shoppingCartQueryWrapper);
        return ShoppingCartEnum.SUCCESS.getResultResponseVo().setData(shoppingCarts);
    }

    /**
     * 购物车中减少商品
     * @param id
     * @return
     */
    @Override
    @Transactional
    public ResultResponseVo subById(Long id) {
        Map<String, Object> map = new HashMap<>();
        ShoppingCart shoppingCart = shoppingCartMapper.selectById(id);
        Integer number = shoppingCart.getNumber();
        if (number<=1){
            int deleteById = shoppingCartMapper.deleteById(id);
            if (deleteById>0) return ShoppingCartEnum.UPDATE_OK.getResultResponseVo();
        }else {
            shoppingCart.setNumber(number-1);
            int updateById = shoppingCartMapper.updateById(shoppingCart);
            map.put("number", number-1);
            if (updateById>0) return ShoppingCartEnum.UPDATE_OK.getResultResponseVo().setData(map);
        }
        return ShoppingCartEnum.UPDATE_FAIL.getResultResponseVo();
    }

    /**
     * 清空购物车
     * @param request
     * @return
     */
    @Override
    public ResultResponseVo cleanCart(HttpServletRequest request) {
        Long userId = (Long) request.getSession().getAttribute(USER_SESSION);
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        shoppingCartQueryWrapper.eq("user_id",userId);
        int delete = shoppingCartMapper.delete(shoppingCartQueryWrapper);
        if (delete>0){
            return ShoppingCartEnum.CLEAN_OK.getResultResponseVo();
        }
        return ShoppingCartEnum.CLEAN_FaIL.getResultResponseVo();
    }
}
